package com.naiterui.ehp.bs.pc.modules.sys.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.pc.modules.sys.entity.RoleMenu;
import com.naiterui.ehp.bs.pc.modules.sys.mapper.RoleMenuMapper;
import com.naiterui.ehp.bs.pc.modules.sys.service.IRoleMenuService;

/**
 * 角色与菜单对应关系
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        // 先删除角色与菜单关系
        this.deleteBatch(new Long[]{roleId});
        if (menuIdList.isEmpty()) {
            return;
        }
        // 保存角色与菜单关系
        this.saveBatch(menuIdList.stream().map(menuId -> new RoleMenu(roleId, menuId)).collect(Collectors.toList()));
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {
        return this.baseMapper.queryMenuIdList(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        this.baseMapper.deleteBatch(roleIds);
    }

}
